// pages/personal/manageAdress/addAdress/addAdress.js
import api from '../../../../utils/api'
let apis = api.api
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		checked: false,
		mobileInp: null,
		addrInp: null,
		addrXxInp: null,
		nameInp: null,
		isDefault: true,
		adrCont: '',
		default: 0,
	},
	onChange({
		detail
	}) {
		// event.detail 为当前输入的值
		// console.log(event.detail)
		var that = this;
		if (detail) {
			that.setData({
				default: 1,
			})
		} else {
			that.setData({
				default: 0,
			})
		}
		this.setData({
			checked: detail
		});
	},
	nameInp(e) {
		this.setData({
			nameInp: e.detail.value
		})
	},
	addrInp(e) {
		this.setData({
			addrInp: e.detail.value
		})
	},
	addrXxInp(e) {
		this.setData({
			addrXxInp: e.detail.value
		})
	},
	mobileInp(e) {
		this.setData({
			mobileInp: e.detail.value,
		})
	},
	getLocation() {
		this.setData({
			adrCont: wx.getStorageSync('myLocal').starAddr,
			addrXxInp: wx.getStorageSync('myLocal').starAddr
		})
	},
	save() {
		var userName = this.data.nameInp;
		var mobile = this.data.mobileInp;
		var phonetel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
		var name = /^[u4E00-u9FA5]+$/;
		if (userName == '') {
			wx.showToast({
				title: '请输入用户名',
				icon: 'succes',
				duration: 1000,
				mask: true
			})
			return false
		} else if (mobile == '') {
			wx.showToast({
				title: '手机号不能为空',
			})

			return false
		} else if (mobile.length != 11) {
			wx.showToast({
				title: '手机号长度有误！',
				icon: 'success',
				duration: 1500
			})
			return false;
		}

		var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
		if (!myreg.test(mobile)) {
			wx.showToast({
				title: '手机号有误！',
				icon: 'success',
				duration: 1500
			})
			return false;
		} else {
			// https://dageda.api.hjdz.ltd/member/add_address
			let that = this
			let url = apis.host + 'member/add_address';
			let isAddr = that.data.addrXxInp
			let data = {
				mobile: mobile,
				/* 是	string	无 */
				address: that.data.addrInp + ' ' + `${isAddr?isAddr:false}`,
				/* 是	string	无 */
				name: userName,
				/* 是	string	无 */
				is_default: that.data.default,
				/* 否	string	默认0 0非默认 1默认地址 */
			}
			apis.request(url, data, 'POST').then(function(res) {
				console.log('新增成功', res)
				wx.showToast({
					title: '新增成功',
					icon: 'success'
				})
				setTimeout(function(res) {
					wx.navigateTo({
						url: '/pages/personal/manageAdress/index/index'
					})
				}, 1000)
			}).catch(function(res) {
				wx.showToast({
					title: '新增失败',
					icon: 'none'
				})
			})
		}

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},
})
